package controller;

import com.sun.javafx.collections.MappingChange;
import dao.AdminDao;
import domain.Adminitem;
import domain.FamilyBookItem;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.control.cell.MapValueFactory;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.util.Callback;
import jdk.nashorn.internal.codegen.CompilerConstants;
import tools.JDBCUtils;
import view.Detile;

import javax.swing.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author :wn
 * @program : 管家婆项目
 * @descript : 应用面板控制器类
 * @create :2020-06-13 15:14
 */

public class AdminFrame {
    @FXML
   public TableView userview;
    @FXML
    public TableColumn number;
    @FXML
    public TableColumn type;
    @FXML
    public TableColumn days;
    @FXML
    public TableColumn descriptions;
    @FXML
    public TableColumn money;
    @FXML
    public TableColumn<SimpleObjectProperty, Button> choose;
    @FXML
    private Button finder;
    @FXML
    private TextField in;
    @FXML
    private TextField out;
    @FXML
    private TextField finderdescription;
    @FXML
    private TextArea finderframe;

    public void seeInOut() throws SQLException, ClassNotFoundException {
        Statement smt=null;
        ResultSet rs=null;
        String url="jdbc:mysql://localhost:3306/familybook?useSSL=false";
        Connection conn= JDBCUtils.getConnection(url);
        smt=conn.createStatement();
        String sql="select * from bill ";
        rs=smt.executeQuery(sql);
        double innumber = 0;
        double outnumber=0;
        while(rs.next()){
            if(rs.getString("taketype").equals("收入")){
                innumber+=rs.getFloat("money");
            }
            in.setText(Double.toString(innumber));
            if(rs.getString("taketype").equals("支出")){
                outnumber+=rs.getFloat("money");
            }
            out.setText(Double.toString(outnumber));
        }
    }


    public void fillTableView() throws ClassNotFoundException, SQLException {
        Adminitem adminitem = new Adminitem();
        ObservableList<Adminitem> date = AdminDao.quary(adminitem);
        number.setCellValueFactory(new PropertyValueFactory<>("number"));
        number.setCellFactory(TextFieldTableCell.forTableColumn());
        type.setCellValueFactory(new PropertyValueFactory<>("type"));
        type.setCellFactory(TextFieldTableCell.forTableColumn());
        days.setCellValueFactory(new PropertyValueFactory<>("days"));
        money.setCellValueFactory(new PropertyValueFactory<>("money"));
        descriptions.setCellValueFactory(new PropertyValueFactory<>("description"));
        descriptions.setCellFactory(TextFieldTableCell.forTableColumn());
        choose.setCellValueFactory(new PropertyValueFactory<>("detile"));
        userview.setItems(date);
        userview.setEditable(true);
    }
    public void finder() throws SQLException, ClassNotFoundException, ParseException {
        Statement smt = null;
        ResultSet rs = null;
        String url = "jdbc:mysql://localhost:3306/familybook?useSSL=false";
        Connection conn = JDBCUtils.getConnection(url);
        smt = conn.createStatement();
       String sql="select * from bill where descriptions like '%"+finderdescription.getText()+"%'";
        rs = smt.executeQuery(sql);
        finderframe.setText("序号       类型       金额       日期             备注 \n");
      while(rs.next()) {
            int id=rs.getInt("id");
            String type=rs.getNString("taketype");
            float money=rs.getFloat("money");
            String money1=Float.toString(money);
            Date date=rs.getDate("days");
            String description=rs.getString("descriptions");
            finderframe.appendText(id+"\t "+"    "+type+"\t "+money1+"  "+date.toString()+"\t"+      description+"\n");
        }
        if(rs!=null){
            rs.close();
        }
        if(smt!=null){
            smt.close();
        }
    }
    public void aboutthesofeware() throws IOException {
        Process process=Runtime.getRuntime().exec("C:\\WINDOWS\\system32\\notepad.exe  src\\管家婆软件说明.txt ");

    }
    public void newBuilder() throws Exception {
        view.AdminFrame adminFrame = new view.AdminFrame();
        adminFrame.start(new Stage());
    }
    public void save() throws IOException {
        if(!finderframe.getText().equals("")) {
            FileOutputStream outputStream = new FileOutputStream("src\\保存test.txt");
            outputStream.write(finderframe.getText().getBytes());
            outputStream.close();
            JOptionPane.showMessageDialog(null,"保存成功！！\n请在相应文件中查看数据");
        }else{
            JOptionPane.showMessageDialog(null,"请先查询想要保存的数据");
        }
    }
    public void leave(){
       view.AdminFrame.stage.close();
   }

}






